﻿using System;
using System.Data;
using System.Data.SqlClient;
using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.CustomizeToolbox;
using DevExpress.XtraEditors;
using ComboBox = System.Windows.Forms.ComboBox;

namespace Dictionary.ERP
{
    public class DIC_JOB
    {
        private bool m_Active;
        private string m_Description;
        private string m_JobCode;
        private string m_JobName;

        public DIC_JOB()
        {
            m_JobCode = "";
            m_JobName = "";
            m_Description = "";
            m_Active = true;
        }

        public DIC_JOB(string JobCode, string JobName, string Description, bool Active)
        {
            m_JobCode = JobCode;
            m_JobName = JobName;
            m_Description = Description;
            m_Active = Active;
        }

        public bool Active
        {
            get { return m_Active; }
            set { m_Active = value; }
        }

        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }

        public string JobCode
        {
            get { return m_JobCode; }
            set { m_JobCode = value; }
        }

        public string JobName
        {
            get { return m_JobName; }
            set { m_JobName = value; }
        }

        public string ProductName
        {
            get { return "Class DIC_JOB"; }
        }

        public string ProductVersion
        {
            get { return "1.0.0.0"; }
        }

        public void AddCombo(ComboBox combo)
        {
            GridCalendarControlLib.TableToComboBox(combo, GetList(), "JobName", "JobCode");
        }

        public void AddComboAll(ComboBox combo)
        {
            var list = new DataTable();
            list = GetList();
            DataRow row = list.NewRow();
            row["JobCode"] = "All";
            row["JobName"] = "Tất cả";
            list.Rows.InsertAt(row, 0);
            GridCalendarControlLib.TableToComboBox(combo, list, "JobName", "JobCode");
        }

        public void AddComboEdit(ComboBoxEdit combo)
        {
            var list = new DataTable();
            list = GetList();
            foreach (DataRow row in list.Rows)
            {
                combo.Properties.Items.Add(row["JobName"]);
            }
        }

        public string Delete()
        {
            string[] myParams = {"@JobCode"};
            object[] myValues = {m_JobCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_JOB_Delete", myParams, myValues);
        }

        public string Delete(string JobCode)
        {
            string[] myParams = {"@JobCode"};
            object[] myValues = {JobCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_JOB_Delete", myParams, myValues);
        }

        public string Delete(SqlConnection myConnection, string JobCode)
        {
            string[] myParams = {"@JobCode"};
            object[] myValues = {JobCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_JOB_Delete", myParams, myValues);
        }

        public string Delete(SqlTransaction myTransaction, string JobCode)
        {
            string[] myParams = {"@JobCode"};
            object[] myValues = {JobCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_JOB_Delete", myParams, myValues);
        }

        public bool Exist(string JobCode)
        {
            bool hasRows = false;
            string[] myParams = {"@JobCode"};
            object[] myValues = {JobCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_JOB_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public string Get(string JobCode)
        {
            string str = "";
            string[] myParams = {"@JobCode"};
            object[] myValues = {JobCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_JOB_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_JobCode = Convert.ToString(reader["JobCode"]);
                    m_JobName = Convert.ToString(reader["JobName"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    m_Active = Convert.ToBoolean(reader["Active"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlConnection myConnection, string JobCode)
        {
            string str = "";
            string[] myParams = {"@JobCode"};
            object[] myValues = {JobCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "DIC_JOB_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_JobCode = Convert.ToString(reader["JobCode"]);
                    m_JobName = Convert.ToString(reader["JobName"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    m_Active = Convert.ToBoolean(reader["Active"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlTransaction myTransaction, string JobCode)
        {
            string str = "";
            string[] myParams = {"@JobCode"};
            object[] myValues = {JobCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "DIC_JOB_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_JobCode = Convert.ToString(reader["JobCode"]);
                    m_JobName = Convert.ToString(reader["JobName"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    m_Active = Convert.ToBoolean(reader["Active"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public DataTable GetList()
        {
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("DIC_JOB_GetList");
        }

        public string Insert()
        {
            string[] myParams = {"@JobCode", "@JobName", "@Description", "@Active"};
            object[] myValues = {m_JobCode, m_JobName, m_Description, m_Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_JOB_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction)
        {
            string[] myParams = {"@JobCode", "@JobName", "@Description", "@Active"};
            object[] myValues = {m_JobCode, m_JobName, m_Description, m_Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_JOB_Insert", myParams, myValues);
        }

        public string Insert(string JobCode, string JobName, string Description, bool Active)
        {
            string[] myParams = {"@JobCode", "@JobName", "@Description", "@Active"};
            object[] myValues = {JobCode, JobName, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_JOB_Insert", myParams, myValues);
        }

        public string Insert(SqlConnection myConnection, string JobCode, string JobName, string Description, bool Active)
        {
            string[] myParams = {"@JobCode", "@JobName", "@Description", "@Active"};
            object[] myValues = {JobCode, JobName, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_JOB_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction, string JobCode, string JobName, string Description,
            bool Active)
        {
            string[] myParams = {"@JobCode", "@JobName", "@Description", "@Active"};
            object[] myValues = {JobCode, JobName, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_JOB_Insert", myParams, myValues);
        }

        public string NewID()
        {
            return SqlHelper.GenCode("DIC_JOB", "JobCode", "CV");
        }

        public string Update()
        {
            string[] myParams = {"@JobCode", "@JobName", "@Description", "@Active"};
            object[] myValues = {m_JobCode, m_JobName, m_Description, m_Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_JOB_Update", myParams, myValues);
        }

        public string Update(string JobCode, string JobName, string Description, bool Active)
        {
            string[] myParams = {"@JobCode", "@JobName", "@Description", "@Active"};
            object[] myValues = {JobCode, JobName, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_JOB_Update", myParams, myValues);
        }

        public string Update(SqlConnection myConnection, string JobCode, string JobName, string Description, bool Active)
        {
            string[] myParams = {"@JobCode", "@JobName", "@Description", "@Active"};
            object[] myValues = {JobCode, JobName, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_JOB_Update", myParams, myValues);
        }

        public string Update(SqlTransaction myTransaction, string JobCode, string JobName, string Description,
            bool Active)
        {
            string[] myParams = {"@JobCode", "@JobName", "@Description", "@Active"};
            object[] myValues = {JobCode, JobName, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_JOB_Update", myParams, myValues);
        }
    }
}